﻿<template>
  <div class="body">
    <div id="main">
      <div class="content" id="lcontent">
      </div>
      <div class="content" id="rcontent">
        <el-form id="loginform" ref="ruleFormRef" style="max-width: 600px;" label-position="left" :model="ruleForm"
          :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
          <div id="title">
            <p>用户登录</p>
          </div>
          <div id="username-content">
            <el-form-item label="用户名：" prop="name">
              <el-input style="width: 240px;" maxlength="10" v-model="ruleForm.name" placeholder="请输入用户名" />
            </el-form-item>
          </div>
          <div id="password-content">
            <el-form-item label="密码：" prop="key">
              <el-input style="width: 240px;" type="password" maxlength="10" :show-password="true"
                v-model="ruleForm.key" placeholder="请输入密码" />
            </el-form-item>
          </div>
          <div id="botton">
            <button id="login-button" @click="submitForm(ruleFormRef)">登录</button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router';

const router = useRouter();
interface RuleForm {
  name: string
  key: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  key: ''
})

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
  ],
  key: [
    { required: true, min: 6, max: 10, message: '请输入6-10位密码', trigger: 'blur' },
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.name === 'admin' && ruleForm.key === '123456') {
        ElMessage.success("登录成功");
         event?.preventDefault();
        router.push({ name: 'AdminHome' }); // 确保此处使用路由名称
      } else {
        ElMessage.error("用户名或密码错误");
      }
    } else {
      ElMessage.error("登录失败")
      console.log('error submit!', fields)
    }
  })
}

</script>
<style scoped>
.body {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: rgba(95, 158, 160, 0.5);
  border: 0px solid burlywood;
  width: 100%;
  height: 100%;
  border-radius: 8px;
}

#main {
  margin-top: 220px;
  background-color: cadetblue;
  display: flex;
  flex-direction: row;
  width: 50%;
  height: 50%;
  border: 1px solid black;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 5px 5px 10px 1px cadetblue;
}

.content {
  width: 50%;
  height: 100%;
}

#main>#lcontent {
  background-color: cadetblue;
  background-image: url("@/assets/car.svg");
  background-repeat: no-repeat;
  background-size: cover;
}

#main>#rcontent {
  background-color: #fffdf8;
}

#loginform {
  box-sizing: border-box;
  border: 0px solid black;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#loginform>#title {
  width: 100%;
  height: 30%;
  border: 0 solid black;
  padding-top: 40px;
  padding-left: 20px;
  font-size: 30px;
  font-weight: bold;
}

#loginform>#username-content,
#password-content {

  width: 100%;
  height: 20%;
  border: 0 solid black;
  padding-top: 15px;
  display: flex;
  flex-direction: row;
  justify-content: center;
  flex-wrap: wrap;
  align-items: center;
}

#loginform>#botton {

  width: 100%;
  height: 30%;
  border: 0 solid black;
  padding-top: 50px;
  text-align: center;
}

#login-button {
  width: 75%;
  height: 40px;
  border-radius: 8px;
  border: none;
}

#login-button:hover {
  background-color: cadetblue;
}
</style>
